<!DOCTYPE html>












  


<html class="theme-next pisces use-motion" lang="zh-CN">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="google-site-verification" content="fjDZF2ivELjrZI5niPw1zuWUx-5Ta0Iy9unh0KSTioM">
























<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2">

<link rel="stylesheet" href="/css/main.css?v=7.1.1">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.1.1">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.1.1">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=7.1.1">


  <link rel="mask-icon" href="/images/logo.svg?v=7.1.1" color="#222">







<script id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Pisces',
    version: '7.1.1',
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false,"dimmer":false},
    back2top: true,
    back2top_sidebar: false,
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="写在前面一般的管网图是不在web端绘制，因为麻烦，一般是用离线处理。不过有时候会有一些特殊的情况，就像我现在在做的一样【汗颜】成果图 项目准备canvas1234567getContext():获取渲染上下文，可理解为获取画布对象，参数设置为2D.fillText(text,x,y):在指定的(x,y)位置填充指定的文本text.beginPath():新建一条路径，生成之后，图形绘制命令被指向到">
<meta name="keywords" content="JS,canvas">
<meta property="og:type" content="article">
<meta property="og:title" content="Vue项目canvas绘制水网管道图">
<meta property="og:url" content="https://sean1224.github.io/2019/05/25/canvas绘制水网管道图/index.html">
<meta property="og:site_name" content="時光深巷夜叶猫">
<meta property="og:description" content="写在前面一般的管网图是不在web端绘制，因为麻烦，一般是用离线处理。不过有时候会有一些特殊的情况，就像我现在在做的一样【汗颜】成果图 项目准备canvas1234567getContext():获取渲染上下文，可理解为获取画布对象，参数设置为2D.fillText(text,x,y):在指定的(x,y)位置填充指定的文本text.beginPath():新建一条路径，生成之后，图形绘制命令被指向到">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2019-05-25T08:47:46.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Vue项目canvas绘制水网管道图">
<meta name="twitter:description" content="写在前面一般的管网图是不在web端绘制，因为麻烦，一般是用离线处理。不过有时候会有一些特殊的情况，就像我现在在做的一样【汗颜】成果图 项目准备canvas1234567getContext():获取渲染上下文，可理解为获取画布对象，参数设置为2D.fillText(text,x,y):在指定的(x,y)位置填充指定的文本text.beginPath():新建一条路径，生成之后，图形绘制命令被指向到">





  
  
  <link rel="canonical" href="https://sean1224.github.io/2019/05/25/canvas绘制水网管道图/">



<script id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>Vue项目canvas绘制水网管道图 | 時光深巷夜叶猫</title>
  












  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">時光深巷夜叶猫</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">

    
    
    
      
    

    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-categories">

    
    
    
      
    

    

    <a href="/categories/" rel="section"><i class="menu-item-icon fa fa-fw fa-th"></i> <br>分类</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">

    
    
    
      
    

    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-tags">

    
    
    
      
    

    

    <a href="/tags/" rel="section"><i class="menu-item-icon fa fa-fw fa-tags"></i> <br>标签</a>

  </li>
        
        
        
          
          <li class="menu-item menu-item-about">

    
    
    
      
    

    

    <a href="/about/" rel="section"><i class="menu-item-icon fa fa-fw fa-user"></i> <br>关于</a>

  </li>

      
      
    </ul>
  

  

  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
            

          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://sean1224.github.io/2019/05/25/canvas绘制水网管道图/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="li jian">
      <meta itemprop="description" content="出走半生，归来仍是少年">
      <meta itemprop="image" content="/images/avatar.png">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="時光深巷夜叶猫">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Vue项目canvas绘制水网管道图

              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              

              
                
              

              <time title="创建时间：2019-05-25 11:30:50 / 修改时间：16:47:46" itemprop="dateCreated datePublished" datetime="2019-05-25T11:30:50+08:00">2019-05-25</time>
            

            
              

              
            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/canvas/" itemprop="url" rel="index"><span itemprop="name">canvas</span></a></span>

                
                
                  ，
                
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/canvas/gis/" itemprop="url" rel="index"><span itemprop="name">gis</span></a></span>

                
                
              
            </span>
          

          
            
            
              
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
            
                <span class="post-meta-item-text">评论数：</span>
                <a href="/2019/05/25/canvas绘制水网管道图/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2019/05/25/canvas绘制水网管道图/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="写在前面"><a href="#写在前面" class="headerlink" title="写在前面"></a>写在前面</h2><p>一般的管网图是不在web端绘制，因为麻烦，一般是用离线处理。不过有时候会有一些特殊的情况，就像我现在在做的一样【汗颜】<a href="../img/canvasechart1.png"><strong>成果图</strong></a></p>
<h2 id="项目准备"><a href="#项目准备" class="headerlink" title="项目准备"></a>项目准备</h2><h3 id="canvas"><a href="#canvas" class="headerlink" title="canvas"></a>canvas</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">getContext():获取渲染上下文，可理解为获取画布对象，参数设置为2D.</span><br><span class="line">fillText(text,x,y):在指定的(x,y)位置填充指定的文本text.</span><br><span class="line">beginPath():新建一条路径，生成之后，图形绘制命令被指向到路径上生成路径。</span><br><span class="line">closePath():闭合路径之后图形绘制命令又重新指向到上下文中。</span><br><span class="line">moveTo(x,y):将笔触移动到指定的坐标x以及y上。</span><br><span class="line">lineTo(x,y):绘制一条从当前位置到指定x以及y位置的直线。</span><br><span class="line">stroke():通过线条来绘制图形轮廓。</span><br></pre></td></tr></table></figure>
<h3 id="vue"><a href="#vue" class="headerlink" title="vue"></a>vue</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">项目为vue项目，数据由父组件传递，通过props获取,图像在mounted函数中渲染。</span><br></pre></td></tr></table></figure>
<p>相关vue知识参考 <a href="https://cn.vuejs.org/v2/guide/" target="_blank" rel="noopener">vue官网</a></p>
<h2 id="项目结构"><a href="#项目结构" class="headerlink" title="项目结构"></a>项目结构</h2><p>页面所有代码如下<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;canvas</span><br><span class="line">  id=&quot;canvas&quot;</span><br><span class="line">  width=&quot;1800px&quot;</span><br><span class="line">  height=&quot;800px&quot;</span><br><span class="line">  style=&quot;border: 1px solid #ebebed;width: 900px;height: 400px;&quot;</span><br><span class="line">  &gt;&lt;/canvas&gt;</span><br><span class="line">&lt;/template&gt;</span><br><span class="line">&lt;script&gt;</span><br><span class="line">import axios from &quot;axios&quot;;</span><br><span class="line">export default &#123;</span><br><span class="line">  name: &quot;chart&quot;,</span><br><span class="line">  data() &#123;</span><br><span class="line">    return &#123;</span><br><span class="line">      config: &#123;</span><br><span class="line">        scale: 2,</span><br><span class="line">        width: 1800,</span><br><span class="line">        height: 800,</span><br><span class="line">        Left: 70,</span><br><span class="line">        Top: 90,</span><br><span class="line">        Right: 30,</span><br><span class="line">        Bottom: 60,</span><br><span class="line">        color: [&quot;#5266d7&quot;],</span><br><span class="line">        yLine: &#123;</span><br><span class="line">          splitcount: 8,</span><br><span class="line">          data: []</span><br><span class="line">        &#125;,</span><br><span class="line">        xLine: &#123;</span><br><span class="line">          splitcount: 9,</span><br><span class="line">          data: []</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;,</span><br><span class="line">      res: null //保存管网点数据</span><br><span class="line">    &#125;;</span><br><span class="line">  &#125;,</span><br><span class="line">  props: &#123;</span><br><span class="line">    pointData: Array,</span><br><span class="line">    lineData: Array //这里获取的是管网的点与线数据，二者是同一条管网</span><br><span class="line">  &#125;, </span><br><span class="line">  methods: &#123;</span><br><span class="line">    getContext: () =&gt; &#123;</span><br><span class="line">      let canvas = document.getElementById(&quot;canvas&quot;);</span><br><span class="line">      let ctx = canvas.getContext(&quot;2d&quot;);</span><br><span class="line">      return ctx;</span><br><span class="line">    &#125;,</span><br><span class="line">    /*</span><br><span class="line">     *画出管道</span><br><span class="line">     *data 保存的点数据</span><br><span class="line">     *perX x轴单元长度</span><br><span class="line">     *perY y轴单元长度</span><br><span class="line">     */</span><br><span class="line">     drawpipeNetwork: function(data, perX, perY) &#123;</span><br><span class="line">      var ctx = this.getContext();</span><br><span class="line">      var config = this.config;</span><br><span class="line">      ctx.beginPath();</span><br><span class="line">      ctx.lineWidth = &quot;60&quot;; </span><br><span class="line">      ctx.strokeStyle = &quot;#ccc&quot;;</span><br><span class="line">      //函数纵坐标减去30是stroke宽度为60</span><br><span class="line">      ctx.moveTo(</span><br><span class="line">        data[0].x * perX + config.Left,</span><br><span class="line">        config.height - config.Bottom - (data[0].y - 1) * perY - 30</span><br><span class="line">        );</span><br><span class="line">      data.forEach(function(item) &#123;</span><br><span class="line">        ctx.lineTo(</span><br><span class="line">          item.x * perX + config.Left,</span><br><span class="line">          config.height - config.Bottom - (item.y - 1) * perY - 30</span><br><span class="line">          );</span><br><span class="line">      &#125;);</span><br><span class="line">      ctx.stroke();</span><br><span class="line">      ctx.closePath();</span><br><span class="line">    &#125;,</span><br><span class="line">    drawEarth:function() &#123;</span><br><span class="line">     let ctx = this.getContext();</span><br><span class="line">     let config =this.config;   </span><br><span class="line">     ctx.beginPath();</span><br><span class="line">     ctx.lineWidth=&apos;1&apos;;</span><br><span class="line">     ctx.strokeStyle=&apos;rgba(141, 103, 0,0.4)&apos;;</span><br><span class="line">     ctx.fillStyle=&apos;rgba(141, 103, 0,0.4)&apos;;</span><br><span class="line">     ctx.moveTo(config.Left,(config.height-config.Bottom)); </span><br><span class="line">       ctx.lineTo(config.Left,(config.height-config.Bottom));//左下角</span><br><span class="line">       ctx.lineTo(config.Left,250);//左上角</span><br><span class="line">       ctx.lineTo((config.width-config.Right),250);//右上角</span><br><span class="line">       ctx.lineTo((config.width-config.Right),(config.height-config.Bottom));//右下角</span><br><span class="line">       ctx.lineTo(config.Left,(config.height-config.Bottom));//左下角</span><br><span class="line">       ctx.stroke();</span><br><span class="line">       ctx.fill()</span><br><span class="line">       ctx.closePath();</span><br><span class="line">     &#125;,</span><br><span class="line">    /*</span><br><span class="line">     *画出最低水位</span><br><span class="line">     *data 保存的点数据</span><br><span class="line">     *perX x轴单元长度</span><br><span class="line">     *perY y轴单元长度</span><br><span class="line">     */</span><br><span class="line">     drawLowLine: function(data, perX, perY) &#123;</span><br><span class="line">      var ctx = this.getContext();</span><br><span class="line">      var config = this.config;</span><br><span class="line">      ctx.beginPath();</span><br><span class="line">      ctx.lineWidth = &quot;2&quot;;</span><br><span class="line">      ctx.strokeStyle = &quot;blue&quot;;</span><br><span class="line">      ctx.moveTo(</span><br><span class="line">        data[0].x * perX + config.Left,</span><br><span class="line">        config.height - config.Bottom - (data[0].y - 1) * perY</span><br><span class="line">        );</span><br><span class="line">      data.forEach(function(item) &#123;</span><br><span class="line">        ctx.lineTo(</span><br><span class="line">          item.x * perX + config.Left,</span><br><span class="line">          config.height - config.Bottom - (item.y - 1) * perY</span><br><span class="line">          );</span><br><span class="line">      &#125;);</span><br><span class="line">      ctx.stroke();</span><br><span class="line">      ctx.closePath();</span><br><span class="line">    &#125;,</span><br><span class="line">    /*</span><br><span class="line">     *画出最高水位</span><br><span class="line">     *data 保存的点数据</span><br><span class="line">     *perX x轴单元长度</span><br><span class="line">     *perY y轴单元长度</span><br><span class="line">     */</span><br><span class="line">     drawHighLine: function(data, perX, perY) &#123;</span><br><span class="line">      var ctx = this.getContext();</span><br><span class="line">      var config = this.config;</span><br><span class="line">      ctx.beginPath();</span><br><span class="line">      ctx.lineWidth = &quot;2&quot;;</span><br><span class="line">      ctx.strokeStyle = &quot;red&quot;;</span><br><span class="line">      ctx.moveTo(</span><br><span class="line">        data[0].x * perX + config.Left,</span><br><span class="line">        config.height -</span><br><span class="line">        config.Bottom -</span><br><span class="line">        (data[0].y - 1 + parseFloat(data[0][&quot;maxDepth&quot;])) * perY</span><br><span class="line">        );</span><br><span class="line">      data.forEach(function(item) &#123;</span><br><span class="line">        ctx.lineTo(</span><br><span class="line">          item.x * perX + config.Left,</span><br><span class="line">          config.height -</span><br><span class="line">          config.Bottom -</span><br><span class="line">          parseFloat(item.y - 1 + parseFloat(item[&quot;maxDepth&quot;])) * perY</span><br><span class="line">          );</span><br><span class="line">      &#125;);</span><br><span class="line">      ctx.stroke();</span><br><span class="line">      ctx.closePath();</span><br><span class="line">    &#125;,</span><br><span class="line">    /*</span><br><span class="line">     *画出坐标X轴</span><br><span class="line">     *@color 颜色</span><br><span class="line">     *@splitcount 分割单元数</span><br><span class="line">     *@linewidth 线宽</span><br><span class="line">     *@data x轴数据源</span><br><span class="line">     */</span><br><span class="line">     drawBackLineY: function(color, splitcount, linewidth, data) &#123;</span><br><span class="line">      var config = this.config;</span><br><span class="line">      var len = Math.floor(</span><br><span class="line">        (config.height - config.Top - config.Bottom) / splitcount</span><br><span class="line">        );</span><br><span class="line">      var start = config.Top;</span><br><span class="line">      for (var i = 0; i &lt;= splitcount; i++) &#123;</span><br><span class="line">        var point = start + len * i;</span><br><span class="line">        this.drawLine(</span><br><span class="line">          config.Left,</span><br><span class="line">          point,</span><br><span class="line">          (config.width / 2) * config.scale - config.Right,</span><br><span class="line">          point,</span><br><span class="line">          color,</span><br><span class="line">          linewidth</span><br><span class="line">          ); </span><br><span class="line">        this.drawText(</span><br><span class="line">          data[splitcount - i],</span><br><span class="line">          30,</span><br><span class="line">          point,</span><br><span class="line">          &quot;normal 22px Arial&quot;,</span><br><span class="line">          &quot;#605d68&quot;</span><br><span class="line">          );</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    /*</span><br><span class="line">     *画出坐标Y轴</span><br><span class="line">     *@color 颜色</span><br><span class="line">     *@splitcount 分割单元数</span><br><span class="line">     *@linewidth 线宽</span><br><span class="line">     *@data x轴数据源</span><br><span class="line">     */</span><br><span class="line">     drawBackLineX: function(color, splitcount, linewidth, data) &#123;</span><br><span class="line">      var config = this.config;</span><br><span class="line">      var len = Math.ceil(</span><br><span class="line">        (config.width - config.Left - config.Right) / splitcount</span><br><span class="line">        );</span><br><span class="line">      var start = config.Left;</span><br><span class="line">      for (var i = 0; i &lt;= splitcount; i++) &#123;</span><br><span class="line">        var point = start + len * i; </span><br><span class="line">        this.drawLine(</span><br><span class="line">          point,</span><br><span class="line">          config.Top,</span><br><span class="line">          point,</span><br><span class="line">          (config.height / 2) * config.scale - config.Bottom,</span><br><span class="line">          color,</span><br><span class="line">          linewidth</span><br><span class="line">          );</span><br><span class="line">        this.drawText(</span><br><span class="line">          data[i],</span><br><span class="line">          point - 30,</span><br><span class="line">          config.height - 20,</span><br><span class="line">          &quot;normal 22px Arial&quot;,</span><br><span class="line">          &quot;#605d68&quot;</span><br><span class="line">          );</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    /*</span><br><span class="line">     *画线</span><br><span class="line">     *@startX 起始点x轴坐标</span><br><span class="line">     *@startY 起始点y轴坐标</span><br><span class="line">     *@endX 起始点x轴坐标</span><br><span class="line">     *@endY 起始点y轴坐标</span><br><span class="line">     *@color 线条颜色</span><br><span class="line">     *@width 线条宽度</span><br><span class="line">     *@lineJoin 线条连接选项</span><br><span class="line">     */</span><br><span class="line">     drawLine: function(startX, startY, endX, endY, color, width, lineJoin) &#123;</span><br><span class="line">      var ctx = this.getContext();</span><br><span class="line">      ctx.beginPath();</span><br><span class="line">      ctx.moveTo(startX, startY);</span><br><span class="line">      ctx.lineTo(endX, endY);</span><br><span class="line">      if (color) &#123;</span><br><span class="line">        ctx.strokeStyle = color;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      if (width) &#123;</span><br><span class="line">        ctx.lineWidth = width * this.config.scale;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      if (lineJoin) &#123;</span><br><span class="line">        ctx.lineJoin = lineJoin;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      ctx.closePath();</span><br><span class="line">      ctx.stroke();</span><br><span class="line">    &#125;,</span><br><span class="line">    /*</span><br><span class="line">     *写字函数</span><br><span class="line">     *@text 字的内容</span><br><span class="line">     *@x 横坐标</span><br><span class="line">     *@y 纵坐标</span><br><span class="line">     *@font 字体</span><br><span class="line">     *@color 字体颜色</span><br><span class="line">     */</span><br><span class="line">     drawText: function(text, x, y, font, color) &#123;</span><br><span class="line">      var ctx = this.getContext();</span><br><span class="line">      ctx.font = font;</span><br><span class="line">      ctx.fillStyle = color;</span><br><span class="line">      ctx.fillText(text, x, y);</span><br><span class="line">    &#125;,</span><br><span class="line">    /*</span><br><span class="line">     *获取x轴纵坐标的标注数组</span><br><span class="line">     *@value 纵坐标的最大值</span><br><span class="line">     *@splitMount 分割数目</span><br><span class="line">     *@fixed 小数点位数</span><br><span class="line">     */</span><br><span class="line">     getAxiasItem: function(value, splitMount, fixed) &#123;</span><br><span class="line">      let res = [];</span><br><span class="line">      let i = 0;</span><br><span class="line">      do &#123;</span><br><span class="line">        res.push(((value / splitMount) * i).toFixed(fixed));</span><br><span class="line">        i++;</span><br><span class="line">      &#125; while (i &lt;= splitMount);</span><br><span class="line">      return res;</span><br><span class="line">    &#125;,</span><br><span class="line">    /*</span><br><span class="line">     *绘制图例</span><br><span class="line">     *@text 图例内容</span><br><span class="line">     *@x 放置横坐标</span><br><span class="line">     *@y 放置纵坐标</span><br><span class="line">     *@color 图例颜色</span><br><span class="line">     *@linewidth 图例宽度</span><br><span class="line">     */</span><br><span class="line">     drawItem: function(text, x, y, color, linewidth) &#123;</span><br><span class="line">      this.drawText(text, x, y, &quot;normal 22px Arial&quot;, &quot;#605d68&quot;);</span><br><span class="line">      this.drawLine(x + 70, y - 5, x + 120, y - 5, color, linewidth);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;,</span><br><span class="line">  mounted() &#123;</span><br><span class="line">    //使用变量保存下来接口中的点和线的数据</span><br><span class="line">    let pointData = this.pointData;</span><br><span class="line">    let lineData = this.lineData;</span><br><span class="line">    //遍历点的数据获取管网对应各点的坐标参数，保存在数组arr中</span><br><span class="line">    let arr = [];</span><br><span class="line">    let lenX = 0; //x轴长度</span><br><span class="line">    let lenY = 0; //y轴长度</span><br><span class="line">    if (pointData.length != 0) &#123;</span><br><span class="line">      for (let i = 0; i &lt; pointData.length; i++) &#123;</span><br><span class="line">        //获取x轴长度</span><br><span class="line">        if (i &gt; 0) &#123;</span><br><span class="line">          lenX += lineData[i - 1][&quot;长度&quot;];</span><br><span class="line">        &#125;</span><br><span class="line">        //获取y轴长度</span><br><span class="line">        if (pointData[i][&quot;内底标高&quot;] &gt; lenY) &#123;</span><br><span class="line">          lenY = pointData[i][&quot;内底标高&quot;];</span><br><span class="line">        &#125;</span><br><span class="line">        arr.push(&#123;</span><br><span class="line">          x: lenX,</span><br><span class="line">          y: pointData[i][&quot;内底标高&quot;],</span><br><span class="line">          minDepth: pointData[i][&quot;初始深度&quot;],</span><br><span class="line">          maxDepth: pointData[i][&quot;最大深度&quot;]</span><br><span class="line">        &#125;);</span><br><span class="line">      &#125; </span><br><span class="line">      this.res = arr;</span><br><span class="line">      //   画出坐标轴</span><br><span class="line">      let config = this.config;</span><br><span class="line">      this.drawText(&quot;水管剖面图&quot;, 850, 40, &quot;normal 26px Arial&quot;, &quot;#605d68&quot;);</span><br><span class="line">      this.drawItem(&quot;水管&quot;, config.width - 150, 50, &quot;#ccc&quot;, &quot;1&quot;);</span><br><span class="line">      this.drawItem(&quot;高水位&quot;, config.width - 300, 50, &quot;red&quot;, &quot;1&quot;);</span><br><span class="line">      this.drawItem(&quot;低水位&quot;, config.width - 450, 50, &quot;blue&quot;, &quot;1&quot;); </span><br><span class="line">      this.drawEarth();</span><br><span class="line">      this.drawBackLineY(</span><br><span class="line">        &quot;#ccc&quot;,</span><br><span class="line">        config.yLine.splitcount,</span><br><span class="line">        1,</span><br><span class="line">        [1, 2, 3, 4, 5, 6, 7, 8, 9].map(function(x) &#123;</span><br><span class="line">          return (x + &quot;m&quot;);</span><br><span class="line">        &#125;)</span><br><span class="line">        );</span><br><span class="line">      this.drawBackLineX(</span><br><span class="line">        &quot;#ccc&quot;,</span><br><span class="line">        config.xLine.splitcount,</span><br><span class="line">        1,</span><br><span class="line">        this.getAxiasItem(lenX, 9, 0).map(function(x)&#123;</span><br><span class="line">          return (x+&apos;m&apos;)</span><br><span class="line">        &#125;)</span><br><span class="line">        );</span><br><span class="line">      this.drawpipeNetwork(</span><br><span class="line">        this.res,</span><br><span class="line">        ((config.width - config.Left - config.Right) / lenX).toFixed(2),</span><br><span class="line">        ((config.height - config.Bottom - config.Top) / 8).toFixed(4)</span><br><span class="line">        );</span><br><span class="line">      this.drawLowLine(</span><br><span class="line">        this.res,</span><br><span class="line">        ((config.width - config.Left - config.Right) / lenX).toFixed(2),</span><br><span class="line">        ((config.height - config.Bottom - config.Top) / 8).toFixed(4)</span><br><span class="line">        );</span><br><span class="line">      this.drawHighLine(</span><br><span class="line">        this.res,</span><br><span class="line">        ((config.width - config.Left - config.Right) / lenX).toFixed(2),</span><br><span class="line">        ((config.height - config.Bottom - config.Top) / 8).toFixed(4)</span><br><span class="line">        );</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;;</span><br><span class="line">&lt;/script&gt;</span><br><span class="line">&lt;style&gt; </span><br><span class="line">&lt;/style&gt;</span><br></pre></td></tr></table></figure></p>
<h2 id="写在最后"><a href="#写在最后" class="headerlink" title="写在最后"></a>写在最后</h2><p>文章涉及的canvas知识比较浅显，有好的解决方案欢迎打扰。</p>

      
    </div>

    

    
    
    

    

    
      
    
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/JS/" rel="tag"># JS</a>
          
            <a href="/tags/canvas/" rel="tag"># canvas</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/05/19/OpenLayer在vue中的简单使用/" rel="next" title="OpenLayer在vue中的简单使用">
                <i class="fa fa-chevron-left"></i> OpenLayer在vue中的简单使用
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/06/15/OpenLayer动态热力图/" rel="prev" title="OpenLayer动态热力图">
                OpenLayer动态热力图 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  
    <div class="comments" id="comments">
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/images/avatar.png" alt="li jian">
            
              <p class="site-author-name" itemprop="name">li jian</p>
              <div class="site-description motion-element" itemprop="description">出走半生，归来仍是少年</div>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">5</span>
                    <span class="site-state-item-name">日志</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  
                    
                      <a href="/categories/">
                    
                  
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">6</span>
                    <span class="site-state-item-name">分类</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-tags">
                  
                    
                      <a href="/tags/">
                    
                  
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">6</span>
                    <span class="site-state-item-name">标签</span>
                  </a>
                </div>
              
            </nav>
          

          

          

          

          

          
          

          
            
          
          

        </div>
      </div>

      
      <!--noindex-->
        <div class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
            
            
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#写在前面"><span class="nav-number">1.</span> <span class="nav-text">写在前面</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#项目准备"><span class="nav-number">2.</span> <span class="nav-text">项目准备</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#canvas"><span class="nav-number">2.1.</span> <span class="nav-text">canvas</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#vue"><span class="nav-number">2.2.</span> <span class="nav-text">vue</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#项目结构"><span class="nav-number">3.</span> <span class="nav-text">项目结构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#写在最后"><span class="nav-number">4.</span> <span class="nav-text">写在最后</span></a></li></ol></div>
            

          </div>
        </div>
      <!--/noindex-->
      

      

    </div>
  </aside>
  


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">
  
  &copy;
  
  
  <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">li jian</span>

  

  
</div>









        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

    

    
  </div>

  

<script>
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  <script src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>


  


  <script src="/js/utils.js?v=7.1.1"></script>

  <script src="/js/motion.js?v=7.1.1"></script>



  
  


  <script src="/js/affix.js?v=7.1.1"></script>

  <script src="/js/schemes/pisces.js?v=7.1.1"></script>



  
  <script src="/js/scrollspy.js?v=7.1.1"></script>
<script src="/js/post-details.js?v=7.1.1"></script>



  


  <script src="/js/next-boot.js?v=7.1.1"></script>


  

  

  

  
  

<script src="//cdn1.lncld.net/static/js/3.11.1/av-min.js"></script>



<script src="//unpkg.com/valine/dist/Valine.min.js"></script>

<script>
  var GUEST = ['nick', 'mail', 'link'];
  var guest = 'nick,mail,link';
  guest = guest.split(',').filter(function(item) {
    return GUEST.indexOf(item) > -1;
  });
  new Valine({
    el: '#comments',
    verify: false,
    notify: false,
    appId: '1knHEkWhq6iHvNiswIIOHD9t-gzGzoHsz',
    appKey: 'wVGfMbCux8bx8W61eQVjLYzy',
    placeholder: 'Just go go',
    avatar: 'mm',
    meta: guest,
    pageSize: '10' || 10,
    visitor: false,
    lang: '' || 'zh-cn'
  });
</script>




  


  




  

  

  

  

  

  

  

  

  

  

  

  

  

  

</body>
</html>
